package scct.chenhui.forms;

import scct.chenhui.dao.UserDao;
import scct.chenhui.model.User;
import scct.chenhui.utils.Config;

import java.awt.*;
import java.awt.event.*;
import java.sql.DriverManager;
import java.sql.SQLException;
import javax.swing.*;
/*
 * Created by JFormDesigner on Sat Jun 22 18:21:37 CST 2024
 */



/**
 * @author chen
 */
public class LoginForm extends JFrame {
    public LoginForm() {
        initComponents();
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        txtName.setText("tom");
        txtPass.setText("123456");
    }

    private void btnCancel(ActionEvent e) {
        // TODO add your code here
        System.exit(0);
    }

    private void btnLogin(ActionEvent e) {
        // 获取用户名和密码
        String name = txtName.getText();
        String pass = txtPass.getText();
//        使用正则表达式验证用户名和密码是否合法
        if(!name.matches("^\\w{3,12}$") || !pass.matches("^\\w{3,12}$"))
        {
            JOptionPane.showMessageDialog(null,"请输入正确格式的用户名和密码");
            return;
        }
//        创建数据操作对象
        UserDao userDao = new UserDao();
//        调用登陆方法
        User user = userDao.login(name, pass);
//        返回null，说明数据库中没有找到匹配的用户名和密码的用户
        if(user==null)
        {
            JOptionPane.showMessageDialog(null,"用户名或密码错误");
            return;
        }
//        保存登陆的用户信息
        Config.userId = user.getId();
        Config.userName = user.getUsername();
//        显示登陆成功信息
        JOptionPane.showMessageDialog(null,"登录成功，欢迎你！"+user.getUsername());
//        创建主窗体
        MainForm mainForm = new MainForm();
//        显示主窗体
        mainForm.setVisible(true);
        this.dispose();
    }

    private void btnRegister(ActionEvent e) {
        // TODO add your code here
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        // Generated using JFormDesigner Evaluation license - hui chen
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        txtName = new JTextField();
        txtPass = new JPasswordField();
        btnLogin = new JButton();
        btnRegister = new JButton();

        //======== this ========
        setResizable(false);
        setTitle("\u7528\u6237\u767b\u9646");
        setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        setBackground(Color.white);
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u7528\u6237\u767b\u5f55");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 24));
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(210, 35), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u7528\u6237\u540d");
        label2.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(90, 115), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("\u5bc6\u7801");
        label3.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(100, 180), label3.getPreferredSize()));

        //---- txtName ----
        txtName.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        contentPane.add(txtName);
        txtName.setBounds(170, 110, 205, 30);

        //---- txtPass ----
        txtPass.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        contentPane.add(txtPass);
        txtPass.setBounds(170, 175, 205, 30);

        //---- btnLogin ----
        btnLogin.setText("\u767b\u5f55");
        btnLogin.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        btnLogin.addActionListener(e -> btnLogin(e));
        contentPane.add(btnLogin);
        btnLogin.setBounds(110, 240, 90, 35);

        //---- btnRegister ----
        btnRegister.setText("\u6ce8\u518c");
        btnRegister.setFont(new Font(Font.DIALOG, Font.PLAIN, 16));
        btnRegister.addActionListener(e -> btnRegister(e));
        contentPane.add(btnRegister);
        btnRegister.setBounds(265, 240, 85, 35);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        setSize(505, 365);
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    // Generated using JFormDesigner Evaluation license - hui chen
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JTextField txtName;
    private JPasswordField txtPass;
    private JButton btnLogin;
    private JButton btnRegister;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
